package cc.higou.core.controller;

import cc.higou.core.pojo.Buyer;
import cc.higou.core.tools.MD5Utils;
import cc.higou.core.tools.SessionTool;
import cc.higou.core.tools.WebTool;
import cc.higou.service.BuyerService;
import cc.higou.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 说明：
 * 登录的控制器
 *
 * @author:dongxing
 * @time:--
 */
@Controller
public class LoginController {

    @Autowired
    private BuyerService buyerService;

    @Autowired
    private SessionService sessionService;


    @RequestMapping(value = "/login.aspx", method = RequestMethod.GET)
    public String showLogin() {

        return "login";
    }

    @RequestMapping(value = "/login.aspx", method = RequestMethod.POST)
    public String doLogin(HttpServletRequest request, HttpServletResponse response, Model model, String returnUrl, String username, String password) {

        if (username != null) {
            Buyer buyer = buyerService.findByUsername(username);
            if (buyer != null) {
                if (password != null) {
                    if (buyer.getPassword().equals(MD5Utils.str2MD5(password))) {
                        //登录成功
                        if (returnUrl == null || returnUrl.trim().length() == 0) {
                            return "redirect:http://localhost:8083/";
                        }
                        //获取自定义的session的值
                        String xingsessionid = SessionTool.getSession(request, response);
                        //将用户的名和sessionid以某种方式存放到Redis中
                        sessionService.addUsernameToRedis("xingsessionid", username);
                        System.out.println("用户名：" + username);
                        System.out.println("密码：" + password);
                        System.out.println("dologin方法执行了");
                        return "redirect:" + returnUrl;

                    } else {
                        //密码错误
                        model.addAttribute("error", "用户密码错误");
                    }

                } else {
                    //密码不能为空
                    model.addAttribute("error", "密码不能为空");
                }
            } else {
                //请输入正确的用户名和密码
                model.addAttribute("error", "用户名或者密码错误");

            }


        } else {
            //用户名不能为空
            model.addAttribute("error", "用户名不能为空");
        }

        return "login";

    }


    @RequestMapping(value = "/ddd.aspx", method = RequestMethod.GET)
    @ResponseBody
    public String ssShow() {
        String s = WebTool.sendGet("http://www.higou.cc", null, "utf-8");
        return s;
    }

    //ajax的跨域调用
    @RequestMapping(value = "/isLogin.aspx")
    @ResponseBody
    public MappingJacksonValue isLogin(String callback, HttpServletRequest request, HttpServletResponse response) {
        String username = sessionService.getUserFromRedis("xingsessionid");
        System.out.println(SessionTool.getSession(request, response));
        System.out.println("sessionUser:" + username);
        // 将要传回给页面的数据，封装到callback的值为命名的js方法中
        MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(username);
        mappingJacksonValue.setJsonpFunction(callback);
        return mappingJacksonValue;

    }


}
